package scripts;

import api.CommonApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.ResponseData;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

public class TestVerifyCode {
    private static final Logger log = LoggerFactory.getLogger(TestVerifyCode.class);

    private final CommonApi commonApi = new CommonApi();


    @DataProvider
    public Object[][] buildData() throws IOException {
        // 读取json文件
        String jsonStr = FileUtils.readFileToString(new File("./data/verifyCode.json"), "UTF-8");

        // 解析json数据，组装数据
        JSONArray jsonArray = JSONObject.parseArray(jsonStr);
        Object[][] testData = new Object[jsonArray.size()][];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Object type = jsonObject.get("type");
            Object statusCode = jsonObject.get("statusCode");
            testData[i] = new Object[]{type, statusCode};
        }
        log.info("testData={}", Arrays.deepToString(testData));
        return testData;
    }


    // 获取图片验证码
    @Test(dataProvider = "buildData")
    public void testVerifyCode(String type, int statusCode) throws Exception {
        // 测试数据
        String random = "";
        if ("float".equals(type)) {
            random = String.valueOf(new Random().nextDouble());
        } else if ("int".equals(type)) {
            random = String.valueOf(new Random().nextInt());
        } else if ("char".equals(type)) {
            random = UUID.randomUUID().toString();
        } else if ("empty".equals(type)) {
            random = "";
        }

        // 发送请求
        ResponseData responseData = commonApi.getVerifyCode(random);

        // 断言
        Assert.assertEquals(responseData.getCode(), statusCode);
    }


    // 随机数为小数
    //@Test
    public void test01VerifyCodeForFloat() throws Exception {
        // 测试数据
        String random = String.valueOf(new Random().nextDouble());
        log.info("random={}", random);

        // 发送请求
        ResponseData responseData = commonApi.getVerifyCode(random);

        // 断言
        Assert.assertEquals(responseData.getCode(), 200);
    }

    //随机数为整数
    //@Test
    public void test02VerifyCodeForInt() throws Exception {
        // 测试数据
        String random = String.valueOf(new Random().nextInt());
        System.out.println("min==" + Integer.MIN_VALUE);
        System.out.println("min==" + Integer.MAX_VALUE);
        log.info("random={}", random);

        // 发送请求
        ResponseData responseData = commonApi.getVerifyCode(random);

        // 断言
        Assert.assertEquals(responseData.getCode(), 200);
    }

    //随机数为字符
    //@Test
    public void test03VerifyCodeForChar() throws Exception {
        // 测试数据
        String random = UUID.randomUUID().toString();
        String random2 = RandomStringUtils.randomAlphabetic(10);
        log.info("random={}", random);
        log.info("random2={}", random2);

        // 发送请求
        ResponseData responseData = commonApi.getVerifyCode(random);

        // 断言
        Assert.assertEquals(responseData.getCode(), 400);
    }

    //随机数为空
    //@Test
    public void test04VerifyCodeForEmpty() throws Exception {
        // 测试数据
        String random = "";

        // 发送请求
        ResponseData responseData = commonApi.getVerifyCode(random);

        // 断言
        Assert.assertEquals(responseData.getCode(), 404);
    }

}
